package org.daidai.abstrusewriting.homepage.classify.client;

import android.util.Log;

import com.google.gson.Gson;

import org.daidai.abstrusewriting.homepage.classify.model.Album;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyItem;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyList;
import org.daidai.abstrusewriting.homepage.classify.model.Special;
import org.daidai.abstrusewriting.homepage.classify.model.SpecialDetail;
import org.daidai.abstrusewriting.homepage.classify.model.SpecialList;
import org.daidai.abstrusewriting.homepage.classify.model.TabItem;
import org.daidai.abstrusewriting.homepage.classify.model.TabItemList;
import org.daidai.abstrusewriting.homepage.classify.model.Track;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2016/10/31.
 */

public class ClientAPI {
    private static final String TAG = "ClientAPI";
    private  static AbstractHttpUtil httpUtil;
    static {
        httpUtil=new OkHttpUtilImpl();

    }

    public static void getClassifyList(){
        String url="http://mobile.ximalaya.com/mobile/discovery/v1/categories?channel=and-a1&device=android&picVersion=13&scale=2";

        httpUtil.doGetDataAsync(url, new HttpCallback() {
            @Override
            public void onSuccess(String url, int code, byte[] data) {
                if (code == 200) {
                    if (data != null) {
                        try {
                             String str = new String(data, "UTF-8");
                             JSONObject jsonObject = new JSONObject(str);
                             JSONArray jsonList = jsonObject.getJSONArray("list");
                             List<ClassifyItem> mList=new ArrayList<ClassifyItem>();

                            for (int i = 0; i < jsonList.length(); i++) {
                                  JSONObject object = jsonList.getJSONObject(i);
                                  Gson gson = new Gson();
                                  ClassifyItem item = gson.fromJson(object.toString(), ClassifyItem.class);
                                  mList.add(item);

                            }

                            List<ClassifyList> classifies=new ArrayList<ClassifyList>();
                            int k=0;

                            int m=mList.size()/6;
                            for (int i = 0; i < m ; i++) {
                                 ClassifyList classfyList= new ClassifyList();
                                 List<ClassifyItem> items=new ArrayList<ClassifyItem>();
                                 items.add(mList.get(i*6+1));
                                 items.add(mList.get(i*6+2));
                                 items.add(mList.get(i*6+3));
                                 items.add(mList.get(i*6+4));
                                 items.add(mList.get(i*6+5));
                                 items.add(mList.get(i*6+6));
                                 classfyList.setClassifyItems(items);
                                 classifies.add(classfyList);
                             }

                            if(m*6<mList.size()-1){
                                ClassifyList classfyList= new ClassifyList();
                                List<ClassifyItem> items=new ArrayList<ClassifyItem>();
                                for (int i = m*6+1; i < mList.size() ; i++) {
                                     items.add(mList.get(i));

                                }
                                classfyList.setClassifyItems(items);

                                classifies.add(classfyList);
                            }
                            //Log.d(TAG, "onSuccess: "+classifies.toString());
                            EventBus.getDefault().post(classifies);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        });
    }


    public static void getTabItemList(long id){
        String url="http://mobile.ximalaya.com/mobile/discovery/v1/category/keywords?categoryId=" +
                String.valueOf(id)+"&channel=and-f6&contentType=album&device=android&statEvent=pageview%2Fcategory%40%E5%A8%B1%E4%B9%90&statModule=%E5%A8%B1%E4%B9%90&statPage=tab%40%E5%8F%91%E7%8E%B0_%E5%88%86%E7%B1%BB&version=5.4.45";


        Log.d(TAG, "getTabItemList: "+ url);

        httpUtil.doGetDataAsync(url, new HttpCallback() {
            @Override
            public void onSuccess(String url, int code, byte[] data) {

                try {
                    String str = new String(data, "UTF-8");
                    JSONObject jsonObject = new JSONObject(str);
                    JSONArray jsonArray = jsonObject.getJSONArray("keywords");
                    List<TabItem> tabItems=new ArrayList<>();

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = jsonArray.getJSONObject(i);
                        Gson gson = new Gson();
                        TabItem tabItem = gson.fromJson(object.toString(), TabItem.class);
                        tabItems.add(tabItem);


                    }
                    TabItemList tabItemList = new TabItemList();
                    tabItemList.setTabItems(tabItems);

                    EventBus.getDefault().post(tabItemList);

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    public static void getSpecialList(int categoryId,long keywordId,int pageId,int pageSize){
    String url="http://mobile.ximalaya.com/mobile/discovery/v2/category/keyword/albums?calcDimension=hot&categoryId=%d&device=android&keywordId=%d&pageId=%d&pageSize=%d&version=5.4.45";
    String formatUrl=String.format(Locale.CHINA,url,categoryId,keywordId,pageId,pageSize);

    httpUtil.doGetDataAsync(formatUrl, new HttpCallback() {
        @Override
        public void onSuccess(String url, int code, byte[] data) {

            try {

                String str = new String(data, "UTF-8");
                JSONObject jsonObject = new JSONObject(str);
                JSONArray jsonArr = jsonObject.getJSONArray("list");

                List<Special> specials=new ArrayList<Special>();
                for (int i = 0; i < jsonArr.length(); i++) {
                    JSONObject jsonObject1 = jsonArr.getJSONObject(i);
                    Gson gson = new Gson();
                    Special special = gson.fromJson(jsonObject1.toString(), Special.class);
                    specials.add(special);

                }
                SpecialList specialList = new SpecialList();
                specialList.setSpecials(specials);
                EventBus.getDefault().post(specialList);


            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
}

    public static void getSpecialDetail(int albumId,int pageId,int pageSize ){
        String url="http://mobile.ximalaya.com/mobile/v1/album?albumId=" +
                String.valueOf(albumId)+"&device=android&isAsc=true&pageId=" +
                String.valueOf(pageId)+"&pageSize=" +
                String.valueOf(pageSize)+"&pre_page=0&source=5&statEvent=pageview%2Falbum%404756811&statModule=%E6%9C%80%E7%81%AB&statPage=categorytag%40%E6%9C%89%E5%A3%B0%E4%B9%A6_%E6%82%AC%E7%96%91&statPosition=1";

        httpUtil.doGetDataAsync(url, new HttpCallback() {
            @Override
            public void onSuccess(String url, int code, byte[] data) {

                try {
                    String str = new String(data, "UTF-8");
                    JSONObject jsonObject = new JSONObject(str);
                    JSONObject dataJson = jsonObject.getJSONObject("data");
                    JSONObject albumJson = dataJson.getJSONObject("album");
                    JSONObject tracksJson = dataJson.getJSONObject("tracks");
                    JSONArray listJson = tracksJson.getJSONArray("list");

                    Gson gson=new Gson();
                    Album album = gson.fromJson(albumJson.toString(), Album.class);

                    SpecialDetail specialDetail = new SpecialDetail();
                    specialDetail.setAlbum(album);
                    List<Track> tracks=new ArrayList<Track>();
                    for (int i = 0; i < listJson.length(); i++) {
                        JSONObject object = listJson.getJSONObject(i);
                        Track track = gson.fromJson(object.toString(), Track.class);
                        tracks.add(track);

                    }
                    specialDetail.setTracks(tracks);
                    //Log.d(TAG, "onSuccess: "+specialDetail.toString());

                    EventBus.getDefault().post(specialDetail);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

}
